﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace NHibernateToSQLMapping.DAL
{
    public class SQLServer2000DAL : SQLServerDAL
    {
        /// <summary>
        /// 查询所有字段属性
        /// </summary>
        /// <param name="conStr">连接字符串</param>
        /// <returns></returns>
        public override DataSet GetTableInfo(string conStr, string tableName)
        {
            string sql = @"select TableName=d.name,[Description]=isnull(f.value,'')
FROM sysobjects d
left join sysproperties f on d.id=f.id and f.smallid=0
where  d.name=@objname
select ColumnName=a.name
, DataType= b.name
, [Length]=COLUMNPROPERTY(a.id,a.name,'PRECISION')
, IsPrimarykey=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '1' else '0' end
, [Description]=isnull(g.[value],'')
, AllowNulls=case when a.isnullable=1 then '1'else '0' end
,IsIdentity=COLUMNPROPERTY(a.id,a.name,'IsIdentity')
FROM syscolumns a
left join systypes b on a.xusertype=b.xusertype
inner join sysobjects d on a.id=d.id
left join syscomments e on a.cdefault=e.id
left join sysproperties g on a.id=g.id and a.colid=g.smallid  
left join sysproperties f on d.id=f.id and f.smallid=0
where  d.name=@objname
order by IsPrimarykey desc";
            SqlParameter[] values = {
                                    new SqlParameter("@objname",tableName)};
            List<string> lst = new List<string>();
            DataSet ds = SqlHelper.ExecuteGetDataSet(conStr, System.Data.CommandType.Text, sql, values);
            return ds;
        }
    }
}
